<%@page import="model.person.Person"%>
<%@page import="utils.TextUtils"%>
<%@page import="org.springframework.web.bind.annotation.RequestParam"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>

<%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

<jsp:include page="../header.jsp" />
<table>
	<tr>
		<td style="min-width: 500px">
			<h1>
				<c:choose>
					<c:when test="${viewingPerson.getAccountID() eq loggedUser.getAccountID()}">
						<strong>Ja</strong>
					</c:when>
					<c:otherwise>
						<strong>${viewingPerson.getName()} ${viewingPerson.getSurname()}</strong>
					</c:otherwise>
				</c:choose>
			</h1>
		</td>
		<td><c:if test="${viewingPerson.getInterest()!=''}">
		Interesuje się:<br>
				<ul>
					<%
						Person viewingPerson = (Person) session.getAttribute("viewingPerson");
							String tab[] = viewingPerson.getInterest().split(", ");
							for (String var : tab) {
								out.print("<li>" + TextUtils.getWithFirstLetterUpperCase(var) + "</li>");
							}
					%>
				</ul>
			</c:if></td>
	</tr>
</table>
<table>
	<tr>
		<td style="min-width: 100px;"><i>${viewingPerson.getSex()}</i></td>
		<c:if test="${viewingPerson.getHome()!=''}">
		<td style="min-width: 100px;">Miejsce zamieszkania: <i>${viewingPerson.getHome() }</i></td>
		</c:if>
	</tr>
</table>

<br>

<c:if test="${not loggedUser.isFriendWith(viewingPerson) and loggedUser.getAccountID()!=viewingPerson.getAccountID()}">
	<a href="addToFriends.html?id=${viewingPerson.getAccountID()}">Dodaj do znajomych!</a>
</c:if>
<br>
<br>
Napisz nowy post:
<br>
<form method="post">
	<!-- input type="text" name="postContent" maxlength="160" style="width: 100%; text-align: center;" -->
	<textarea name="postContent" rows="3" cols="50" style="max-width: 85%; max-height: 80px; min-height: 10px; min-width: 50%">Nowy post</textarea>
	<input type="submit" value="Wyślij" />
</form>
<br>
<hr>
<br>
<c:if test="${listOfPosts.size() eq 0}">
	Nie ma postów
</c:if>
<c:if test="${param.start eq null}">
	<%
		request.setAttribute("start", 0);
	%>
</c:if>

<c:if test="${param.start >= listOfPosts.size()}">
	<%
		response.sendRedirect("profile.html?id=" + request.getParameter("id"));
	%>
</c:if>

<table>

	<c:forEach var="post" items="${listOfPosts}" begin="${param.start}" end="${param.start+4}" step="1">
		<tr>
			<td align="left"><c:choose>
					<c:when test="${post.getSourcePerson().getAccountID() eq loggedUser.getAccountID()}">
						<strong>Ja</strong>
					</c:when>
					<c:otherwise>
						<strong>${post.getSourcePerson().getName()} ${post.getSourcePerson().getSurname()}</strong>
					</c:otherwise>
				</c:choose></td>
			<td align="right">${post.getCreatedDateString()}</td>
		</tr>
		<tr height="200">
			<!-- td>Treść:</td -->
			<td width="20%" align="center" valign="baseline" colspan="2">${post.getPostContent()}</td>
		</tr>
		<tr>
			
			<td align="right" colspan="2">
				<c:if test="${ loggedUser.getAccountID() eq post.getDestinationPerson().getAccountID() or loggedUser.getAccountID() eq post.getSourcePerson().getAccountID() or loggedUser.hasRole('admin')}">
					<a href="profile.html?id=${post.getDestinationPerson().getAccountID()}&deleteId=${post.getId()}">Usuń</a>
				</c:if>
			</td>
		</tr>

		<tr height="30">
			<td colspan="2">
				<hr>
			</td>
		</tr>
	</c:forEach>
</table>


<table>
	<tr>
		<td width="500px" nowrap><c:if test="${param.start-5>=0}">
				<a href="profile.html?id=${viewingPerson.getAccountID()}&start=${param.start-5}">Poprzednie posty</a>
			</c:if></td>
		<td nowrap align="right"><c:if test="${param.start+5<listOfPosts.size()}">
				<a href="profile.html?id=${viewingPerson.getAccountID()}&start=${param.start+5}">Następne posty</a>
			</c:if></td>
	</tr>
</table>

<jsp:include page="../footer.jsp" />
